//
//  LMImageController.m
//  舍品
//
//  Created by 楼 眉 on 16/3/16.
//  Copyright © 2016年 楼 眉. All rights reserved.
//

#import "LMImageController.h"
#import <Masonry.h>

@interface LMImageController ()
@property (nonatomic,weak)  UIImageView *iconImageView;
@end

@implementation LMImageController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.view.backgroundColor = [UIColor blackColor];
}
- (void)setImage:(UIImage *)image
{
    _image = image;
    if (image) {
        double scale = image.size.width / image.size.height;
        UIImageView *iconImageView = [[UIImageView alloc]init];
        iconImageView.image = image;
        iconImageView.width = SCREEN_WIDTH;
        iconImageView.height = SCREEN_WIDTH / scale;
        iconImageView.center = self.view.center;
        self.iconImageView = iconImageView;
        [self.view addSubview:iconImageView];
        iconImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *iconImageViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(back)];
        [iconImageView addGestureRecognizer:iconImageViewTap];
    }else
    {
        NSLog(@"没有图片");
    }
}

- (void)setupUI
{
    UIButton *saveBtn = [[UIButton alloc]init];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    [saveBtn setBackgroundColor:RGBCOLOR(200, 200, 200)];
    [saveBtn.layer setMasksToBounds:YES];
    [saveBtn.layer setCornerRadius:6.0]; //设置矩形四个圆角半径
    [saveBtn.layer setBorderWidth:1.0]; //边框宽度
    [self.view addSubview:saveBtn];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(26);
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(20);
    }];
    
    [saveBtn addTarget:self action:@selector(saveIconImage) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - 点击保存图片按钮
- (void)saveIconImage
{
    UIImageWriteToSavedPhotosAlbum(self.iconImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
#pragma mark - 图片完成储存
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    
    if (error == nil) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已存入手机相册" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil ];
        [alert show];
        
    }else{
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失败" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }
    
}
#pragma mark - 返回上级
- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - didReceiveMemoryWarning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}
#pragma mark - dealloc
- (void)dealloc
{
    NSLog(@"dealloc--imageVc");
}
@end
